package leetcode.list;

/**
 * https://leetcode-cn.com/problems/add-two-numbers/
 *
 * @author mazouri
 * @create 2021-08-18 17:41
 */
public class _02_addTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }

        if (l2 == null) {
            return l1;
        }
        //空结点
        ListNode head = new ListNode(0);
        ListNode last = head;
        int carry = 0;

        while (l1 != null || l2 != null) {
            int v1 = 0;
            if (l1 != null) {
                v1 = l1.val;
                l1 = l1.next;
            }

            int v2 = 0;
            if (l2 != null) {
                v2 = l2.val;
                l2 = l2.next;
            }
            int sum = v1 + v2 + carry;
            //设置进位
            carry = sum / 10;
            // sum的个位数作为新节点的值
            last.next = new ListNode(sum % 10);
            last = last.next;

        }

        if (carry == 1) {
            last.next = new ListNode(1);
        }

        return head.next;
    }
}
